typedef ConvertCallback<T> = T? Function(dynamic item);

/// Tries to convert input [item] using [convertCallback] if not null.
/// Otherwise returns [defaultValue].
T? tryToConvert<T>(
  dynamic item,
  ConvertCallback convertCallback, {
  dynamic defaultValue,
}) {
  if (item == null || (item is String && item.trim().isEmpty)) {
    return defaultValue;
  }

  try {
    return convertCallback(item);
  } catch (_) {
    return defaultValue;
  }
}
